package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

/**
 * @author five-five
 * @createTime 2022/2/28 15:41
 */
public class Panel extends JPanel {
    BufferedImage bg = GetImage.getImage("bg1.jpg");
    Plane plane = new Plane();
    List<EnemyPlane> enemyPlanes = new ArrayList<>();


    public void productEnemyPlanes() {
        new Thread(() -> {
            try {
                while (true) {
                    epCreate();
                    epMove();
//                    TimeUnit.MILLISECONDS.sleep(1000);
                    Thread.sleep(20);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

    }

    private void epMove() {
        enemyPlanes.forEach(enemyPlane -> {
            enemyPlane.y += 5;
        });
    }

    int index = 20;

    private void epCreate() {
        index++;
        if (index >= 20) {
            EnemyPlane enemyPlane = new EnemyPlane();
            enemyPlanes.add(enemyPlane);
        }
        repaint();
    }

    public Panel(Frame frame) {
        //鼠标监听
        planeMoveByMouse();

        //键盘监听
        planeMoveByKeyBoard(frame);
    }

    private void planeMoveByKeyBoard(Frame frame) {
        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
                    plane.x -= 10;
                }
                if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
                    plane.x += 10;
                }
                if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
                    plane.y -= 10;
                }
                if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
                    plane.y += 10;
                }
                if (plane.x < 0) {
                    plane.x = 0;
                }
                if (plane.y < 0) {
                    plane.y = 0;
                }
                if (plane.x + 10 + plane.image.getWidth() > Frame.size_x) {
                    plane.x = Frame.size_x - 10 - plane.image.getWidth();
                }
                if (plane.y + 10 + plane.image.getHeight() > Frame.size_y) {
                    plane.y = Frame.size_y - 10 - plane.image.getHeight();
                }
                repaint();
            }
        };
        frame.addKeyListener(keyAdapter);
    }

    private void planeMoveByMouse() {
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                plane.x = e.getX() - plane.image.getWidth() / 2;
                plane.y = e.getY() - plane.image.getHeight() / 2;
                repaint();
            }
        };
        addMouseListener(mouseAdapter);
        addMouseMotionListener(mouseAdapter);
    }

    /**
     * 背景和飞机设置
     *
     * @param g 画笔
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg, 0, 0, null);
        g.drawImage(plane.image, plane.x, plane.y, null);
        for (int i = 0; i < enemyPlanes.size(); i++) {
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            g.drawImage(enemyPlane.image, enemyPlane.x, enemyPlane.y, null);
        }
    }
}
